import React, { Component } from 'react'
import Search from '@/components/Search'
import HomeSwipe from '@/components/HomeSwipe'
import HomeNoAddress from '@/components/HomeNoAddress'
import HomeNoLogin from '@/components/HomeNoLogin'
import HomeDrawer from '@/components/HomeDrawer'
import HomeList from '@/components/HomeList'
import { libCityListCity_list } from '@/api'
import { connect } from 'react-redux'
import style from './home.module.scss';
import { changeRegionListAction } from '@/store/modules/Region/regionActiveCreate.js'
// import style from './home.module.scss'

class Home extends Component {
    render() {
        return (
            <>
                {this.isaddressFn()}
            </>
        )
    }
    componentDidMount() {
        libCityListCity_list().then((res) => {
            // console.log(res);
            this.props.changeCurrentLocation(res)
        })
    }
    // 判断有没有选择地址
    isaddressFn = () => {
        if (this.props.currentLocation.id || this.props.LocationSelected.id) {
            return (
                <>
                    <Search />
                    <HomeSwipe />
                    {this.isLogin()}
                </>
            )
        } else {
            return (
                <>
                    <Search />
                    <HomeNoAddress />
                </>
            )
        }
    }
    isLogin = () => {
        if (this.props.islogin) {
            return (
                <>
                    <div className={style.screenTitle}>
                        <div className={style.horizontalBar}></div>
                        推荐商家
                        <div className={style.horizontalBar}></div>
                    </div>
                    <HomeDrawer />
                    <HomeList />
                </>
            )
        } else {
            return (
                <HomeNoLogin />
            )
        }
    }
}

const stateToProps = (state) => {
    console.log(state);
    return {
        currentLocation: state.region.currentLocation,
        LocationSelected: state.shipping.LocationSelected,
        islogin: state.user.isLogin
    }
}

const dispatchToProps = (dispatch) => {
    return {
        changeCurrentLocation: (value) => {//修改所在位置
            let action = { value }
            dispatch(changeRegionListAction(action))
        }
    }
}


export default connect(stateToProps, dispatchToProps)(Home)
